﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Advisor_RemoveAdvisee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["isAdmin"] == null || !(Boolean)Session["isAdmin"])
            {
                Page.Response.Redirect("Default.aspx");
            }
            if (Session["Advisor_advisorID"] == null)
                Page.Response.Redirect("Default.aspx");
            populateAdviseeList();
        }
    }
    void populateAdviseeList()
    {
        List<Dictionary<String, Object>> results;
        if ((Boolean)Session["isAdmin"])
        {
            results = StoredProcedures.getAllAdvisees();
        }
        else
        {
            results = StoredProcedures.getAdvisees(Session["Advisor_advisorID"].ToString());
        }
        //Clear the AdviseeList to add the Advisee names
        AdviseeList.Items.Clear();
        foreach(Dictionary<String, Object> i in results)
        {
            ListItem advisee = new ListItem(i["lastName"] + ", " + i["firstName"], (String)i["adviseeID"]);
            AdviseeList.Items.Add(advisee);
        }
    }
    protected void HomeButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Advisor/Default.aspx");
    }
    protected void HelpButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Advisor/Help.aspx");
    }
    protected void CancelButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Advisor/Default.aspx");
    }
    protected void RemoveButton_Click(object sender, EventArgs e)
    {
        if (AdviseeList.SelectedItem == null)
            return;
        if (StoredProcedures.removeAdvisee(AdviseeList.SelectedValue) == 0)
        {
            //error condition
        }
        List<Dictionary<String, Object>> stuffToDelete = StoredProcedures.getNotes(AdviseeList.SelectedValue);
        foreach (Dictionary<String, Object> stuff in stuffToDelete)
        {
            StoredProcedures.removeNote(stuff["noteID"].ToString());
        }
        stuffToDelete = StoredProcedures.getLogEntries(AdviseeList.SelectedValue);
        foreach (Dictionary<String, Object> stuff in stuffToDelete)
        {
            StoredProcedures.removeLogEntry(stuff["logEntryID"].ToString());
        }
        stuffToDelete = StoredProcedures.getScheduleCourseInfo(AdviseeList.SelectedValue);
        foreach (Dictionary<String, Object> stuff in stuffToDelete)
        {
            StoredProcedures.removeCourseFromSchedule(AdviseeList.SelectedValue, stuff["courseID"].ToString());
        }
        stuffToDelete = StoredProcedures.getScheduleElectiveInfo(AdviseeList.SelectedValue);
        foreach (Dictionary<String, Object> stuff in stuffToDelete)
        {
            StoredProcedures.removeElective(stuff["electiveID"].ToString());
        }
        //delete from courseInSchedule, electiveInSchedule, log, notes
        populateAdviseeList();
    }
}